package com.lxy.seata.service.impl;

import com.lxy.seata.dao.TOrderDao;
import com.lxy.seata.entity.TOrder;
import com.lxy.seata.service.TAccountService;
import com.lxy.seata.service.TOrderService;
import com.lxy.seata.service.TStroageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lixinyang
 * @className: TOrderServiceImpl
 * @description: TODO
 * @date 2022/05/02日  19:53
 */

@Service
@Slf4j
public class TOrderServiceImpl implements TOrderService{

    @Resource
    private TOrderDao tOrderDao;

    @Autowired
    private TStroageService tStroageService;

    @Autowired
    private TAccountService tAccountService;

    @Override
    @GlobalTransactional(name = "lxy_seata_test_glo",rollbackFor = Exception.class)
    public void create(TOrder order) {
        //1、创建订单
        log.info("开始创建订单");
        tOrderDao.create(order);

        //2、修改库存
        log.info("开始修改库存");
        int stroage = tStroageService.decrease(order.getProductId(), order.getCount());
        if (stroage == 1) {
            log.info("修改库存成功");
        }else {
            log.info("修改库存失败");
        }


        //3、修改余额
        log.info("开始修改余额");
        int account = tAccountService.decrease(order.getUserId(), order.getMoney());
        if (account == 1) {
            log.info("修改余额成功");
        }else {
            log.info("修改余额失败");
        }

        //4、修改订单状态
        log.info("开始修改修改订单状态");
        tOrderDao.update(order.getUserId(),0);


        log.info("创建订单完成");

    }


}
